<%= header %>
<script>
window.xhrTimes = [0, 2000, 3000, 5000];
</script>
<script src="/test-templates/autoxhr/00-xhrs-duplicate.js" type="text/javascript"></script>
<script src="25-xhrs-duplicate-tao.js" type="text/javascript"></script>
<%= boomerangScript %>
<script>
BOOMR_test.init({
	"instrument_xhr": true,
	testAfterOnBeacon: BOOMR.plugins.AutoXHR ? 5 : 1,
	AutoXHR: {
		alwaysSendXhr: true
	},
	afterFirstBeacon:
		function() {
			if (!BOOMR.plugins.AutoXHR) {
				return;
			}

			// request waterfall, requests overlap (last 3 are same url/query):
			// * (xhr0 ~0s)
			//   ***** (xhr1 ~1s)
			//      ********* (xhr2 ~2s)
			//           ************* (xhr3 ~3s)

			// xhr0 starts, completes and sends a beacon.
			// xhr1 starts and adds a xhr pending event.
			// xhr2 starts.
			// xhr1 completes but will wait until xhr2 completes
			// xhr3 starts.
			// xhr2 completes and sends a beacon (only sent because alwaysSendXhr:true). xhr1 is now also waiting for xhr3 to complete.
			// xhr3 completes and sends a beacon (only sent because alwaysSendXhr:true).
			// xhr1 is no longer waiting for nodes and sends a beacon.

			// use secondary hostname to simulate 3rd party resource
			var urlPrefix = "//<%= secondaryServer %>:" + window.location.port;

			// reset saved server delay to 0
			var xhr = new XMLHttpRequest();
			xhr.open("GET", urlPrefix + "/delay?delay=0&file=build/boomerang-latest-debug.js&TAO=1");
			xhr.send(null);

			BOOMR.subscribe("beacon", function() {
				// send 3 XHRs with the same URL, increasing response time for each request
				var timeout = 0;
				for (var i = 0; i < 3; i++) {
					setTimeout(function(delay) {
						var xhr2 = new XMLHttpRequest();
						// with Timing-Allow-Origin=*
						// NOTE: Use POST, otherwise Chrome delays multiple GET XHRs for the same URL (2 at a time)
						xhr2.open("POST", urlPrefix + "/delay?delay=%2B1000&file=build/boomerang-latest-debug.js&TAO=1");
						xhr2.send(null);
					}, timeout, i * 1000);

					timeout += (i + 0.5) * 1000; // requests will overlap by half a second
				}
			}, null, null, true);
		}
});
</script>
<%= footer %>
